﻿// <copyright file="IconDialog.xaml.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.UI
{
    using System;
    using System.Windows;
    using Microsoft.Win32;
    using SnowmanConsulting.ProductKeyManager.Data;
    using SnowmanConsulting.ProductKeyManager.Logging;

    /// <summary>
    /// Interaction logic for IconDialog.xaml
    /// </summary>
    public partial class IconDialog : Window
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="IconDialog"/> class.
        /// </summary>
        public IconDialog()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(this.IconDialog_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.IconDialog_Closing);
            this.btnOk.Click += new RoutedEventHandler(this.OkButton_Click);
            this.btnCancel.Click += new RoutedEventHandler(this.CancelButton_Click);
            this.btnBrowse.Click += new RoutedEventHandler(this.BrowseButton_Click);

            try
            {
                if (IconDialog.Icons == null)
                {
                    IconDialog.Icons = new ProductIconCollection();
                }

                if (IconDialog.Icons.Count <= 0)
                {
                    ProductIconHelper.LoadDefaultIcons(IconDialog.Icons);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLine(LogItemType.Exception, ex);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the selected icon.
        /// </summary>
        /// <value>The selected icon.</value>
        public ProductIcon SelectedIcon
        {
            get
            {
                if (this.lstIcons.SelectedItem != null)
                {
                    return this.lstIcons.SelectedItem as ProductIcon;
                }
                else
                {
                    if (this.lstIcons.Items.Count >= 1)
                    {
                        return this.lstIcons.Items[0] as ProductIcon;
                    }
                }

                return null;
            }
        }

        /// <summary>
        /// Gets or sets the icons.
        /// </summary>
        /// <value>The icons.</value>
        private static ProductIconCollection Icons
        {
            get
            {
                return Properties.Settings.Default.Icons;
            }

            set
            {
                Properties.Settings.Default.Icons = value;
                Properties.Settings.Default.Save();
            }
        }

        #endregion

        #region Private Methods
        /// <summary>
        /// Handles the Click event of the btnBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.Filter = "Icon file (.ico)|*.ico";

            bool result = (bool)ofd.ShowDialog();

            if (result)
            {
                ProductIcon icon = new ProductIcon { IconUrl = ofd.FileName };

                IconDialog.Icons.Add(icon);
                this.lstIcons.SelectedItem = icon;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        /// <summary>
        /// Handles the Loaded event of the IconDialog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void IconDialog_Loaded(object sender, RoutedEventArgs e)
        {
            this.lstIcons.DataContext = IconDialog.Icons;
        }

        /// <summary>
        /// Handles the Closing event of the IconDialog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void IconDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Logger.WriteLine(LogItemType.Exception, ex);
                throw;
            }
        }
        #endregion
    }
}
